package com.aq.appserver.server.theme.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aq.appserver.common.bean.BaseResp;
import com.aq.appserver.common.bean.PageBean;
import com.aq.appserver.common.bean.Request;
import com.aq.appserver.common.util.DateUtil;
import com.aq.appserver.server.account.entity.HeadImg;
import com.aq.appserver.server.theme.bean.CommentBeanWeb;
import com.aq.appserver.server.theme.bean.ThemeBeanWeb;
import com.aq.appserver.server.theme.entity.Comment;
import com.aq.appserver.server.theme.entity.Theme;
import com.aq.appserver.server.theme.service.ThemeService;

@Controller
@RequestMapping("/theme")
public class ThemeController {
	
	@Autowired
	private ThemeService themeService;
	
	@RequestMapping("queryWebTheme")
    @ResponseBody
	public Object queryWebTheme(@ModelAttribute("themeBeanWeb") ThemeBeanWeb themeBeanWeb, HttpServletRequest request) {
		BaseResp resp = new BaseResp();
		List<ThemeBeanWeb> list = themeService.queryWebTheme(themeBeanWeb);
		resp.put("rows", list);
		resp.put("total", themeService.queryWebThemeCount(themeBeanWeb));
		return resp;
	}
	
	@RequestMapping("queryWebComment")
    @ResponseBody
	public Object queryWebComment(@ModelAttribute("commentBeanWeb") CommentBeanWeb commentBeanWeb, HttpServletRequest request) {
		BaseResp resp = new BaseResp();
		List<CommentBeanWeb> list = themeService.queryWebComment(commentBeanWeb);
		resp.put("rows", list);
		resp.put("total", list.size());
		return resp;
	}
	
	@RequestMapping("toTopWebTheme")
    @ResponseBody
	public Object toTopWebTheme(@ModelAttribute("themeBeanWeb") ThemeBeanWeb themeBeanWeb, HttpServletRequest request) {
		BaseResp resp = new BaseResp();
		//查询已经置顶的帖子数目
		int count = themeService.queryTopNum();
		if(count < 3) {
			themeService.toTopWebTheme(themeBeanWeb);
			resp.put("flag", "0");
		} else {
			resp.put("flag", "1");
			resp.put("desc", "已经置顶3个帖子,不能置顶新的帖子。");
		}
		return resp;
	}
	
	@RequestMapping("cancerTopWebTheme")
    @ResponseBody
	public Object cancerTopWebTheme(@ModelAttribute("themeBeanWeb") ThemeBeanWeb themeBeanWeb, HttpServletRequest request) {
		BaseResp resp = new BaseResp();
		themeService.cancerTopWebTheme(themeBeanWeb);
		resp.put("flag", "0");
		return resp;
	}
	
	@RequestMapping("deleteWebTheme")
    @ResponseBody
	public Object deleteWebTheme(@ModelAttribute("themeBeanWeb") ThemeBeanWeb themeBeanWeb, HttpServletRequest request) {
		BaseResp resp = new BaseResp();
		themeService.deleteWebTheme(themeBeanWeb);
		resp.put("flag", "0");
		return resp;
	}
	
	@RequestMapping("deleteWebComment")
    @ResponseBody
	public Object deleteWebComment(@ModelAttribute("commentBeanWeb") CommentBeanWeb commentBeanWeb, HttpServletRequest request) {
		BaseResp resp = new BaseResp();
		themeService.deleteWebComment(commentBeanWeb);
		resp.put("flag", "0");
		return resp;
	}
    @RequestMapping("saveTheme")
    @ResponseBody
	public Object saveTheme(@RequestBody Theme theme, HttpServletRequest request) {
		BaseResp resp = new BaseResp();
		theme.setClickNum(0);
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		theme.setFirstTime(sd.format(new Date()));
		theme.setUpdateTime(new Date());
		themeService.saveTheme(theme);
		return resp;
	}
    
    @RequestMapping("saveContent")
    @ResponseBody
	public Object saveContent(@RequestBody Comment content, HttpServletRequest request) {
		BaseResp resp = new BaseResp();
		content.setUpdateTime(new Date());
		themeService.saveContent(content);
		return resp;
	}
    
    @RequestMapping("updateClickNum")
    @ResponseBody
	public Object updateClickNum(@RequestBody Request<Map<String, Object>> request) {
		BaseResp resp = new BaseResp();
		Map<String, Object> themeInfo = request.getParameter();
		String themeId = themeInfo.get("id").toString();
		themeService.updateClickNum(themeId);
		return resp;
	}
    
    @RequestMapping("queryThemeList")
    @ResponseBody
	public Object queryThemeList(@RequestBody Request<Map<String, Object>> request) {
		BaseResp resp = new BaseResp();
		Map<String, Object> themeInfo = request.getParameter();
		PageBean pageBean = request.getPageBean();
		Pageable pageable = new PageRequest(pageBean.getCurrentPage() - 1, pageBean.getPageSize(), new Sort(Direction.DESC, "topTime", "updateTime"));
		//Pageable pageable = new PageRequest(0, 15, new Sort(Direction.DESC, "id"));
		String title = themeInfo == null ? "" : (themeInfo.get("title") == null ? "" : themeInfo.get("title").toString());
		String phoneNum = themeInfo == null ? "" : (themeInfo.get("phoneNum") == null ? "" : themeInfo.get("phoneNum").toString());
		Page<Theme>  themeList = null;
		if (StringUtils.isEmpty(phoneNum)) {
			if (!StringUtils.isEmpty(title)) {
				themeList = themeService.queryThemeList("%" + title + "%", pageable);
			} else {
				themeList = themeService.queryThemeList(pageable);
			}
		} else {
			if (!StringUtils.isEmpty(title)) {
				themeList = themeService.queryThemeListByPhoneNum(phoneNum,"%" + title + "%", pageable);
			} else {
				themeList = themeService.queryThemeListByPhoneNum(phoneNum, pageable);
			}
		}
		
		if (themeList != null && themeList.getSize() >0) {
			List<Theme> themeInfoList = themeList.getContent();
			if (themeInfoList != null && themeInfoList.size() > 0) {
				for (Theme theme : themeInfoList) {
					//只计算其他用户回复
					int replyNum = themeService.getReplyNum(String.valueOf(theme.getId()));
					theme.setReplyNum(replyNum);
					
					//设置头像
					List<HeadImg>  headImgList = themeService.queryHeadImgByPhoneNum(theme.getPhoneNum());
					if (headImgList != null && headImgList.size() > 0) {
						HeadImg headImg = headImgList.get(0);
						theme.setHeadUrl(headImg.getUrl());
						theme.setHeadType(headImg.getType());
					}
				}
			}
			
			resp.put("content", themeInfoList);
			resp.put("isLast", themeList.isLast() ? "1" : "0");
		}
		return resp;
	}
    
    @RequestMapping("queryContentList")
    @ResponseBody
	public Object queryContentList(@RequestBody Request<Map<String, Object>> request) {
		BaseResp resp = new BaseResp();
		Map<String, Object> contentInfo = request.getParameter();
		PageBean pageBean = request.getPageBean();
		Pageable pageable = new PageRequest(pageBean.getCurrentPage() - 1, pageBean.getPageSize(), new Sort(Direction.ASC, "id"));
		String themeId = contentInfo.get("themeId") == null ? "" : contentInfo.get("themeId").toString();
		
		if (StringUtils.isEmpty(themeId) ) {
			resp.setResp("9999", "主题已经不存在。");
		} else {
			Page<Comment>  contentList = themeService.findByThemeId(themeId, pageable);
			if (contentList != null && contentList.getSize() >0) {
				List<Comment> contentInfoList = contentList.getContent();
				if (contentInfoList != null && contentInfoList.size() > 0) {
					for (Comment contentPad : contentInfoList) {
						//设置头像
						List<HeadImg>  headImgList = themeService.queryHeadImgByPhoneNum(contentPad.getPhoneNum());
						if (headImgList != null && headImgList.size() > 0) {
							HeadImg headImg = headImgList.get(0);
							contentPad.setHeadUrl(headImg.getUrl());
							contentPad.setHeadType(headImg.getType());
						}
					}
					resp.put("replyContent", contentInfoList);
					resp.put("isLast", contentList.isLast() ? "1" : "0");
				}
			}
		}
		
		return resp;
    }
		
}
